﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;
using FileBasedObjectRepository;

namespace NewsMine.FeedRepository
{
    public class FileBasedWebSiteStore 
    {
        private GenericFileDataStore<WebSiteInfo> webSiteRepository { get; set; }

        public FileBasedWebSiteStore(string rootFolder)
        {
            webSiteRepository = new GenericFileDataStore<WebSiteInfo>(rootFolder);
        }

        public WebSiteInfo AddWebSiteInfo(WebSiteInfo webSiteInfo)
        {
            return webSiteRepository.Store(webSiteInfo);
        }

        public bool SaveWebSiteInfo(WebSiteInfo webSiteInfo)
        {
            try
            {
                webSiteRepository.Store(webSiteInfo);
            }
            catch (Exception ex)
            {
                //todo: Need to handle the exception.

                return false;
            }

            return true;
        }

        public WebSiteInfo GetWebSiteByID(long id)
        {
            return webSiteRepository.GetById<WebSiteInfo>(id);
        }

        public List<WebSiteInfo> GetWebSiteByDomain(string domain)
        {
            List<WebSiteInfo> results = new List<WebSiteInfo>();
            for (int i = 1; i < 100000; i++)
            {
                var web = webSiteRepository.GetById<WebSiteInfo>(i);

                if (web == null)
                    continue;

                if (web != null && !string.IsNullOrEmpty(web.Domain) && web.Domain.ToLower().Contains(domain.ToLower()))
                {
                    results.Add(web);
                }
            }

            return results;
        }

        public List<WebSiteInfo> GetWebSitesByLauguage(string lauguage)
        {
            var webs = GetAllWebSites();

            List<WebSiteInfo> filteredList = new List<WebSiteInfo>();

            foreach (var website in webs)
            {
                if (website!=null && !string.IsNullOrEmpty(website.Language) && website.Language.ToLower().Contains(lauguage.ToLower()))
                {
                    filteredList.Add(website);
                }
            }

            return filteredList;
        }

        public List<WebSiteInfo> GetAllWebSites()
        {
            List<WebSiteInfo> webs = webSiteRepository.GetObjects(1, 100000);

            return webs;
        }

        public void ReadDataFromCSVFiles()
        {
            throw new ApplicationException("This needs to be implemented;.....");

        }

        //**** Below are not implemented. **** //
        #region not implemented methods 


        //public WebSiteMenu AddWebMenuItem(WebSiteMenu webSiteMenu)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetAllAdultWebSites()
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetAllInactiveWebsites()
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteMenu> GetMenuItemById(long webSiteMenuID)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteMenu> GetMenuListByWebSite(WebSiteInfo websiteInfo)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetRelatedWebSites(WebSiteInfo websiteInfo)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByAuthor(AuthorInfo author)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByAuthorID(long authorID)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByCategory(Category category)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByCategoryID(long categoryId)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByGeoLocation(GeoLocation location)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByGeoLocationID(long locationID)
        //{
        //    throw new NotImplementedException();
        //}

        //public WebSiteInfo GetWebSitesByLanguage(Language language)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesByLanguageID(long languageId)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesForCategoryHierarchy(Category category)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> GetWebSitesForUser(UserInfo userInfo)
        //{
        //    throw new NotImplementedException();
        //}

       

        //public bool SaveWebSiteMenu(WebSiteMenu websiteMenu)
        //{
        //    throw new NotImplementedException();
        //}

        //public List<WebSiteInfo> SearchWebSitesByName(string name)
        //{
        //    throw new NotImplementedException();
        //}

        #endregion

    }
}
